<?php

include_once(APP_URL.'protected/lib/private/Check.php');
include_once(APP_URL.'protected/lib/private/Str.php');

class UtilPath {


    public static function convert($projectId, $prjObj) {

        if(!$projectId || !$prjObj) {
            echo Resp::retFail(0, 'project id NULL');
            exit;
        }

        // get all project image files name from db
        $condArr = array(
            'select' => array('name','url'),
            'order' => 'id DESC',
            'condition' => 'project_id=:projectId AND type=:type',
            'params' => array('projectId' => $projectId, 'type' => 2)
        );
        $TblProjectFile = new TblProjectFile();
        $result = $TblProjectFile->selectRecordAll($condArr);
        if(!$result) {
            echo Resp::retFail(0, '请上传图片');
            exit;
        }

        $prjOriginNodeId = $prjObj[0];
        $prjPathObj = $prjObj[1];
        $rmPathObj = [];

        // filter & remove all unused node
        foreach($prjPathObj as $key => $value) {
            if(!$value[4]) {
                unset($prjPathObj[$key]);
            }
        }
//        echo var_dump($result);
//        echo json_encode($prjPathObj);exit;

        // fill data to roam pth file
        foreach($prjPathObj as $key => $value) {
            $imageName = $value[0];
            $imageUrl = UtilPath::getImageUrlByName($result, $imageName);
            if(!$imageUrl) {
                echo Resp::retFail(0, '图片'.$imageName.'没有上传');
                exit;
            }
            $rmPathObj[] = array(
                $imageUrl,// true image url of server
                deg2rad($value[3]) * 2,// yaw
                ($key + Str::randomFloat()),
                array(),
                Str::randomFloat()
            );
        }
        $rmObj = array($prjOriginNodeId, $rmPathObj);

        // compute path relationship
        foreach($prjPathObj as $key => $value) {

            $x = $value[1];// x
            $y = $value[2];// y

            $pathArr = $value[4];
            if(!$pathArr) {
                continue;
            }

            foreach ($pathArr as $pathId) {

                $pathObj =  UtilPath::getObj($prjPathObj, $pathId);

                $difX = $pathObj[1] - $x;
                $difY = $pathObj[2] - $y;

                $agl = 0;
                if($difX > 0 && $difY == 0) {
                    $agl = 0;
                } else if($difX > 0 && $difY > 0) {
                    $agl = atan($difY / $difX);
                } else if($difY > 0 && $difX == 0) {
                    $agl = M_PI_2;
                } else if($difY > 0 && $difX < 0) {
                    $agl = atan(-$difX / $difY) + M_PI_2;
                }  else if($difY == 0 && $difX < 0) {
                    $agl = M_PI;
                } else if($difY < 0 && $difX < 0) {
                    $agl = atan($difY / $difX) +M_PI;
                } else if($difY < 0 && $difX == 0) {
                    $agl = M_PI * 1.5;
                } else if($difY < 0 && $difX > 0) {
                    $agl = atan($difX / -$difY) + M_PI * 1.5;
                }

                if($agl <= M_PI) {
                    $agl = M_PI - $agl;
                } else {
                    $agl = 2 * M_PI - ($agl - M_PI);
                }
//                echo rad2deg($agl).'/';
                $naaObj = array(
                    $pathId + Str::randomFloat(),
                    $agl * 3,
                    Str::randomFloat(),
                    $pathId + Str::randomFloat()
                );

                $rmObj[1][$key][3][] = $naaObj;
            }
        }

        return $rmObj;
    }

    private static function getObj($obj, $id) {

        foreach($obj as $key=>$value) {
            if($key == $id) {
                return $value;
            }
        }
        return null;
    }

    private static function getImageUrlByName($arr, $name) {

        foreach ($arr as $value) {
//            echo $name,'/',$value->name;
            if($name == $value->name) {
                if($value->url) {
                    return $value->url;
                }
            }
        }
        return null;
    }

}